<template>
  <dialog-component
    :dialog-visible="state.show"
    size="360px"
    title="报表导出"
    :btn-text="'导出'"
    @close-dialog="handleClose()"
    @save-dialog="handleSubmit()"
  >
    <el-form ref="formRef" :model="state.formData" :rules="state.rules" label-width="auto">
      <el-form-item label="报表名称:"> {{ title }} </el-form-item>
      <el-form-item label="报表密码:" prop="password">
        <el-input v-model="state.formData.password" placeholder="请输入报表密码或直接导出"></el-input>
      </el-form-item>
    </el-form>
  </dialog-component>
</template>

<script lang="ts" setup>
import type { FormInstance, FormRules } from 'element-plus'
import { useProxy } from '@/utils/proxy'
import { debtExport } from '../api'
import { downLoadTemplate } from '@/utils/download'
import { validCode } from '@/utils/validate'
const props = defineProps({
  // eslint-disable-next-line vue/prop-name-casing
  DatEQuery: {
    type: Object,
    default() {
      return {}
    }
  },
  title: {
    type: String,
    default: ''
  },
  userType: {
    type: String,
    default: '0'
  },
  areaCodeList: {
    type: Array,
    default: () => []
  },
  months: {
    type: String,
    default: ''
  },
  tableTitle: {
    type: String,
    default: ''
  }
})
const formRef = ref<FormInstance>()
const { proxy } = useProxy()
const state = reactive({
  isSubmiting: false,
  tableId: '',
  show: false,
  formData: {
    password: ''
  },
  rules: {
    password: [{ validator: validCode, message: '密码只能输入英文和数字', trigger: 'blur' }]
  } as FormRules
})
// 关闭回调
const handleClose = () => {
  state.show = false
  state.tableId = ''
  state.formData = {
    password: ''
    // ...props.DatEQuery
  }
}
// 提交
const handleSubmit = async () => {
  // console.log(state.formData, props.DatEQuery)
  let obj = {
    ...state.formData,
    ...props.DatEQuery
  }
  let res = await debtExport(state.tableId, obj)
  downLoadTemplate(res, Date.now().toString())
  console.log(res)

  state.show = false
}
defineExpose({ state })
</script>

<style lang="scss" scoped></style>
